/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @author danang
 *
 */
public class Cleaner {	
	// nilai delay eksekusi GC
	private final static int DELAY_GC_EXECUTE = 10; 
	
	private class CleanerThread implements Runnable {
		public CleanerThread() {}

		@Override
		public void run() {
			System.out.println("Java GC called");
			System.gc();
		}
		
	}
	
	ScheduledExecutorService fScheduler;
	CleanerThread c;
	public Cleaner() {
		fScheduler = Executors.newSingleThreadScheduledExecutor();
		c = new CleanerThread();
	}
	
	public void activateGC(){
		fScheduler.scheduleWithFixedDelay(c, 20, DELAY_GC_EXECUTE, TimeUnit.SECONDS);
	}
	
	public void terminate() {
		System.out.println("GC terminated.");
		fScheduler.shutdown();
	}
	
}